<template>
	<!-- 54是搜索框整体高度  52是tab的整体高度  不展示搜索框时需去除54-->
	<view class="page" :style="{'min-height':h+'px','padding-top':(mt+54+52)+'px'}">
		<!-- 头部 -->
		<c-nav-bar title="船票订单" backUrl="/pages/my/index"></c-nav-bar>
		<TopTabs :list="tabList" @changeTab="changeTab" :top="mt" :placeholder="'输入订单号'" @sousuo="sousuo"></TopTabs>
		<view class="orders">
			<view v-for="(item,index) in orderList" :key="index" :class="item.orderState==3?'qx':''"
				@tap.prevent="toDetails(item)">
				<view class="top">
					<view class="left">
						<image src="@/static/my/cp_zs.png" mode="" v-if="item.orderState!=3"></image>
						<image src="@/static/my/cp_zs_hs.png" mode="" v-else></image>
						<text>船票</text>
					</view>
					<view class="right">
						<text :class="oscConfig[item.orderState]||'textDcx'">{{ocfg[item.orderState]}}</text>
					</view>
				</view>
				<view class="content">
					<view class="title">
						<text>{{item.startPortName}} - {{item.endPortName}} - {{item.lineNum+''+item.sx}}</text>
						<text>￥{{item.totalPayFee}}</text>
					</view>
					<view class="date">
						<text>全票 X {{item.qpNum}},</text>
						<text v-if="item.etpNum>0">儿童票 X {{item.etpNum}},</text>
						<text>共{{item.qpNum+item.etpNum}}张</text>
					</view>
					<view class="room_price">
						<text>开船时间：{{item.kcTime}}</text>
					</view>
				</view>
				<view class="btns" v-if="['1','2','5'].includes(item.orderState)">
					<view @tap.stop="applyAfterSales(item)" class="btn_blue">申请售后</view>
					<view @tap.stop="refundTicket(item)" class="btn_blue">退票</view>
					<view @tap.stop="changeTicket(item)" class="btn_blue">改签</view>
				</view>
				<view class="btns" v-if="item.orderState==7">
					<view @tap.stop="refundTicket(item)" class="btn_blue">退票</view>
				</view>
				<view class="btns" v-if="item.orderState==0">
					<view @tap.stop="cancelOrder(item)">取消</view>
					<view @tap.stop="toPay(item)" class="btn_blue">去支付</view>
				</view>
				<view class="btns" v-if="item.orderState==20">
					<view @tap.stop="toEvaluate(item)" class="btn_blue">去评价</view>
				</view>
			</view>
		</view>
		<view class="tip">{{orderList.length>0?'已显示全部订单':'暂无订单'}}</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import TopTabs from '../../compoments/TopTabs/topTabs.vue';
	export default {
		name: 'shipTicketOrderIndex',
		components: {
			TopTabs
		},
		data() {
			return {
				mysearch:'',
				h: uni.getSystemInfoSync().windowHeight,
				mt: uni.getSystemInfoSync().statusBarHeight + 44,
				titleStyle: {
					fontSize: '36rpx',
					fontWeight: "bold",
					color: "#333333"
				},
				tabList: [{
						name: '全部'
					},
					{
						name: '待支付'
					},
					{
						name: '待使用'
					},
					{
						name: '退款/售后'
					}
				],
				orderList: [],
				oscConfig: {
					0: 'textDzf',
					1: 'textDcx',
					3: 'textQx',
					5: 'textDzf',
				},
				pageIndex: 1,
				pageCount: 10,
				isOver: false,
				ocfg: this.$ocfg,
				orderState: ''
			}
		},
		onLoad() {
			this.getList();
			// setInterval(() => {
			// 	this.init();
			// }, 5 * 60 * 1000)
		},
		onReachBottom() {
			if (this.isOver) return
			this.getList();
		},
		methods: {
			init() {
				this.pageIndex = 1;
				this.isOver = false;
				this.orderList = [];
				this.getList();
			},
			sousuo(keyword){
				this.mysearch = keyword;
				this.pageIndex = 1;
				this.isOver = false;
				this.orderList = [];
				this.getList();
			},
			
			getList() {
				let that = this;
				this.$api.get('/api/travel/isshiporder/page', {
					openId: JSON.parse(uni.getStorageSync('userInfo')).openId,
					orderState: this.orderState,
					page: this.pageIndex,
					limit: this.pageCount,
					search:this.mysearch,
					orderType:'3,9'
				}, true, false).then(res => {
					if (res.data.code === 0) {
						that.pageIndex++;
						let {
							list,
							total
						} = res.data.data;
						if (that.orderList.length + list.length > total || list.length == 0) return that.isOver =
							true;
						that.orderList = [...that.orderList, ...list];
						that.orderList.forEach((d, i) => {
							d.qpNum = d.detailDTOList.filter(f => f.priceType == 1).length;
							d.etpNum = d.detailDTOList.filter(f => f.priceType == 2).length;
							d.kcTime = new Date(d.sailDate).Format('yyyy年MM月dd日') + ' ' + d.sailTime
						})
					} else that.$showToast(res.data.msg);
				})
			},
			changeTab(index) {
				if (index === 0) this.orderState = '';
				else if (index === 1) this.orderState = '0'
				else if (index === 2) this.orderState = '1,2';
				else this.orderState = '-2,-1,3,4,5,6,7,8,9,10,11';
				this.init();
			},
			//申请售后
			applyAfterSales(item) {
				this.$refs.uToast.show({
					type: 'success',
					title: '',
					message: "正在开发",
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/default.png'
				})
			},
			//退票
			refundTicket(item) {
				uni.navigateTo({
					url: '/pagesMy/shipTicketOrder/refundTicket?item=' + JSON.stringify(item)
				})
			},
			//改签
			changeTicket(item) {
				uni.navigateTo({
					url: '/pagesMy/shipTicketOrder/changeTicket?item=' + JSON.stringify(item)
				})
			},
			//取消订单
			cancelOrder(item) {
				this.$api.post('/travel/api/ship/order/cancel', {
					orderId: item.orderId
				}).then(res => {
					if (res.data.code === 0) {
						this.$showToast('订单已取消');
						this.init();
					} else this.$showModal(res.data.msg)
				})
			},
			//去支付
			toPay(item) {
				this.$api.post('/order/icbc/createOrder', {
					orderCode: item.orderId,
					tradeType: 'JSAPI',
					openId: JSON.parse(uni.getStorageSync('userInfo')).openId
				}, false).then(res => {
					if (res.statusCode == 200 && res.data != null) {
						this.$wxPay(res.data).then(result => {
							this.$showToast('支付成功');
							this.init();
						})
					}
				})
			},
			toDetails(item) {
				uni.redirectTo({
					url: '/pagesMy/shipTicketOrder/details?orderId=' + item.orderId
				})
			},
			returnBack() {
				uni.reLaunch({
					url: '/pages/my/index'
				})
			},
			toEvaluate(item){
				item.guideId = item.id;
				item.sourceType = 3;
				item.orderCode = item.orderId;
				uni.navigateTo({
					url:'/pagesMy/appraise/appraise?obj='+JSON.stringify(item)
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.page {
		background: #F5F8FA;
		padding: 100rpx 0 1rpx;

		.orders {
			display: flex;
			flex-direction: column;
			align-items: center;

			&>view {
				width: calc(100% - 96rpx);
				margin-top: 20rpx;
				border-radius: 16rpx;
				background: #fff;
				padding: 36rpx 24rpx 24rpx;

				.top {
					display: flex;
					justify-content: space-between;

					.left {
						display: flex;
						align-items: center;

						image {
							width: 36rpx;
							height: 36rpx;
						}

						text {
							font-size: 28rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #111111;
							line-height: 28rpx;
							margin-left: 20rpx;
						}
					}

					.right {
						text-align: right;
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						line-height: 37rpx;
					}
				}

				.content {
					padding: 22rpx 0 24rpx 56rpx;
					background: #fff;

					.title {
						display: flex;
						align-items: center;
						justify-content: space-between;

						text {
							font-size: 32rpx;
							font-family: PingFang-SC-Bold, PingFang-SC;
							font-weight: bold;
							color: #333333;
							line-height: 45rpx;

							&:last-child {
								color: #111111;
							}
						}
					}

					.date {
						display: flex;
						align-items: center;
						margin-top: 22rpx;

						&>text {
							font-size: 24rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #AAAAAA;
							line-height: 24rpx;
						}
					}

					.room_price {
						margin-top: 16rpx;
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #AAAAAA;
					}
				}

				.btns {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					border-top: 1rpx solid #EFEFEF;

					&>view {
						margin: 24rpx 0 0 24rpx;
						width: 148rpx;
						height: 56rpx;
						border: 1rpx solid #EFEFEF;
						border-radius: 28rpx;
						line-height: 56rpx;
						text-align: center;
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;

						&.btn_blue {
							border: 1rpx solid #007A69;
							color: #007A69;
						}
					}
				}

				&.qx {
					text {
						color: #AAAAAA !important;
					}
				}
			}
		}

		.tip {
			width: 100%;
			text-align: center;
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #AAAAAA;
			line-height: 33rpx;
			padding: 56rpx 0;
		}

		.textQx {
			color: #AAAAAA;
		}

		.textDcx {
			color: #333333;
		}

		.textDzf {
			color: #FF7D01;
		}
	}
</style>